//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#if !(defined(_GNUC) && defined(_arm))

#include <string.h>
#include <elatypes.h>

EXTERN_C void * CDECL memset(void * pDest, int c, size_t cb)
{
#ifdef _GNUC

#if defined(_x86)
    int temp0, temp1;
    ASM("rep;"
        "stosb;"
        :"=&D" (temp0), "=&c" (temp1)
        :"0" (pDest), "a" (c), "1" (cb)
        :"memory");
    return pDest;
#endif    //if defined(_x86)

#endif    //ifdef _GNUC

    if (((int)pDest | cb) & (sizeof(void *)-1)) { // not natural aligned?
        char* p = (char*) pDest;
        while (cb--) {
            *p++ = (char)c;         // copy one byte at a time
        }
    }
    else {                          // in case of natural alignment
        int* p = (int*) pDest;

//        assert(sizeof(int) == 4);   // NOTE: modify for 64 bit machine later
        if (c != 0) {               // speed up zeroing a buffer
            c |= (c << 8);
            c |= (c << 16);
        }

        cb /= sizeof(int);
        while (cb--) {
            *p++ = c;               // copy 4 bytes at a time
        }
    }
    return pDest;
}

#endif
